<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputPath>$(RuntimeBinDir)ilc/</OutputPath>
    <RuntimeIdentifier>$(PackageRID)</RuntimeIdentifier>
  </PropertyGroup>

  <Import Project="ILCompiler.props" />


  <!-- BEGIN: Workaround for https://github.com/dotnet/runtime/issues/67742 -->
  <PropertyGroup Condition="'$(BuildingInsideVisualStudio)' != 'true'">
    <PublishDir>$(RuntimeBinDir)ilc-published/</PublishDir>
    <NativeAotSupported Condition="$(OutputRID.StartsWith('tizen')) == 'true'">false</NativeAotSupported>
    <NativeAotSupported Condition="$(OutputRID.EndsWith('-arm')) == 'true'">false</NativeAotSupported>
    <NativeAotSupported Condition="$(OutputRID.EndsWith('-x86')) == 'true'">false</NativeAotSupported>
    <!-- Disable native AOT on FreeBSD when cross building from Linux. -->
    <NativeAotSupported Condition="'$(TargetOS)' == 'freebsd' and '$(CrossBuild)' == 'true'">false</NativeAotSupported>
    <PublishAot Condition="'$(NativeAotSupported)' == 'true'">true</PublishAot>
    <SysRoot Condition="'$(NativeAotSupported)' == 'true' and '$(CrossBuild)' == 'true' and '$(HostOS)' != 'windows'">$(ROOTFS_DIR)</SysRoot>
    <PublishReadyToRun Condition="'$(NativeAotSupported)' != 'true'">true</PublishReadyToRun>
    <PublishSingleFile Condition="'$(NativeAotSupported)' != 'true'">true</PublishSingleFile>
    <PublishTrimmed Condition="'$(NativeAotSupported)' != 'true'">true</PublishTrimmed>
    <SuppressGenerateILCompilerExplicitPackageReferenceWarning>true</SuppressGenerateILCompilerExplicitPackageReferenceWarning>
  </PropertyGroup>

  <ItemGroup Condition="'$(NativeAotSupported)' == 'true'">
    <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="$(MicrosoftDotNetILCompilerVersion)" />
    <PackageReference Include="runtime.$(ToolsRID).Microsoft.DotNet.ILCompiler" Version="$(MicrosoftDotNetILCompilerVersion)" />
  </ItemGroup>

  <!-- Needed for the amd64 -> amd64 musl cross-build to pass the target flag. -->
  <Target Name="_FixIlcTargetTriple"
          AfterTargets="SetupOSSpecificProps"
          Condition="'$(CrossBuild)' == 'true' and '$(HostOS)' != 'windows'">
    <!-- Compute CrossCompileRid, and copy the downstream logic as-is. -->
    <PropertyGroup>
      <CrossCompileRid>$(RuntimeIdentifier)</CrossCompileRid>

      <CrossCompileArch />
      <CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-x64'))">x86_64</CrossCompileArch>
      <CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-arm64')) and '$(_IsApplePlatform)' != 'true'">aarch64</CrossCompileArch>
      <CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-arm64')) and '$(_IsApplePlatform)' == 'true'">arm64</CrossCompileArch>

      <TargetTriple />
      <TargetTriple Condition="'$(CrossCompileArch)' != ''">$(CrossCompileArch)-linux-gnu</TargetTriple>
      <TargetTriple Condition="'$(CrossCompileArch)' != '' and ($(CrossCompileRid.StartsWith('linux-musl')) or $(CrossCompileRid.StartsWith('alpine')))">$(CrossCompileArch)-alpine-linux-musl</TargetTriple>
      <TargetTriple Condition="'$(CrossCompileArch)' != '' and ($(CrossCompileRid.StartsWith('freebsd')))">$(CrossCompileArch)-unknown-freebsd12</TargetTriple>
    </PropertyGroup>

    <ItemGroup>
      <LinkerArg Include="--target=$(TargetTriple)" Condition="'$(_IsApplePlatform)' != 'true' and '$(TargetTriple)' != ''" />
    </ItemGroup>
  </Target>

  <Target Name="LocateNativeCompiler"
          Condition="'$(NativeAotSupported)' == 'true' and '$(HostOS)' != 'windows'"
          BeforeTargets="SetupOSSpecificProps">
    <PropertyGroup>
      <CppCompilerAndLinker Condition="'$(CppCompilerAndLinker)' == ''">clang</CppCompilerAndLinker>
    </PropertyGroup>

    <Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(CppCompilerAndLinker)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo &quot;$CC;$LDFLAGS&quot;' 2>/dev/null"
          EchoOff="true"
          ConsoleToMsBuild="true"
          StandardOutputImportance="Low">
      <Output TaskParameter="ConsoleOutput" PropertyName="_CC_LDFLAGS" />
    </Exec>

    <Exec Command="xcodebuild -version" Condition="'$(_IsApplePlatform)' == 'true'" IgnoreExitCode="true" StandardOutputImportance="Low" ConsoleToMSBuild="true">
      <Output TaskParameter="ExitCode" PropertyName="_XcodeVersionStringExitCode" />
      <Output TaskParameter="ConsoleOutput" PropertyName="_XcodeVersionString" />
    </Exec>

    <PropertyGroup Condition="('$(_XcodeVersionStringExitCode)' == '0' or '$(_XcodeVersionStringExitCode)' == '1') and '$(_XcodeVersionString)' != ''">
      <_XcodeVersion>$([System.Text.RegularExpressions.Regex]::Match($(_XcodeVersionString), '[1-9]\d*'))</_XcodeVersion>
    </PropertyGroup>

    <ItemGroup Condition="'$(NativeAotSupported)' == 'true' and '$(_IsApplePlatform)' == 'true'">
      <CustomLinkerArg Condition="'$(_XcodeVersion)' &gt;= '15'" Include="-ld_classic" />
    </ItemGroup>

    <PropertyGroup>
      <CppLinker>$(_CC_LDFLAGS.SubString(0, $(_CC_LDFLAGS.IndexOf(';'))))</CppLinker>
      <_LDFLAGS>$(_CC_LDFLAGS.SubString($([MSBuild]::Add($(_CC_LDFLAGS.IndexOf(';')), 1))))</_LDFLAGS>
      <LinkerFlavor Condition="$(_LDFLAGS.Contains('lld'))">lld</LinkerFlavor>
    </PropertyGroup>
  </Target>

  <ItemGroup Condition="'$(NativeAotSupported)' == 'true'">
    <CustomLinkerArg Condition="'$(CrossBuild)' == 'true' and '$(BuildArchitecture)' == '$(_targetArchitecture)' and '$(HostOS)' != 'windows' and '$(_IsApplePlatform)' != 'true'" Include="--gcc-toolchain=$(ROOTFS_DIR)/usr" />
  </ItemGroup>

  <Target Name="PublishCompiler"
          Condition="'$(BuildingInsideVisualStudio)' != 'true'"
          AfterTargets="Build"
          DependsOnTargets="Publish;StompSingleFileHostPath" />

  <!--  HACK: liveBuilds stomps over SingleFileHostSourcePath, setting it to the host that we just built.
        That's unfortunate because it's not the host we're supposed to use here. -->
  <Target Name="StompSingleFileHostPath"
          BeforeTargets="ResolveFrameworkReferences">
    <PropertyGroup>
      <SingleFileHostSourcePath></SingleFileHostSourcePath>
    </PropertyGroup>
  </Target>
  <!-- END: Workaround for https://github.com/dotnet/runtime/issues/67742 -->
</Project>
